home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req3 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-04-05  |  5KB  |  186 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5. #include "main.h"
  6.  
  7. #include "iff.h"
  8.  
  9. #include<libraries/asl.h>
  10. #include<intuition/intuition.h>
  11. #include<workbench/workbench.h>
  12.  
  13. #include<string.h>
  14. #include<stdio.h>
  15.  
  16. #include<clib/asl_protos.h>
  17. #include<clib/dos_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/icon_protos.h>
  20. #include<clib/intuition_protos.h>
  21.  
  22. /* Global handles for our requesters */
  23. static struct FileRequester* loadreq = NULL;
  24. static struct FileRequester* savereq = NULL;
  25.  
  26. /* Open an ASL load file requester */
  27. int load()
  28. {
  29.     /* Allocate the requester if we haven't already */
  30.     if(loadreq == NULL)
  31.         loadreq = (struct FileRequester*)
  32.             AllocAslRequestTags(ASL_FileRequest,
  33.                                                     ASLFR_TitleText,            "Load File",
  34.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  35.                                                     ASLFR_InitialPattern,    "#?.iff",
  36.                                                     TAG_DONE);
  37.     if(loadreq)
  38.     {
  39.         struct Window* win = getDrawWin();
  40.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  41.         {
  42.             char filename[MAXFILENAME];
  43.             /* Create complete filename from ASL's dir and file */
  44.             strcpy(filename, loadreq->rf_Dir);
  45.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  46.                 return loadfile(filename);
  47.             else
  48.                 printf("Error: could not make filename\n");
  49.         }
  50.         /* else: requester was cancelled */
  51.     }
  52.     else
  53.         printf("Error: could not allocate ASL (load) file request\n");
  54.     /* If we get this far then there was no fatal error */
  55.     return TRUE;
  56. }
  57.  
  58. int loadfile(char* filename)
  59. {
  60.     /* Record any fatal errors */
  61.     int going = TRUE;
  62.     IFFL_HANDLE handle;
  63.     /* Try to open the IFF file */
  64.     if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  65.     {
  66.         UWORD colortable[256];
  67.         /* Get colour information */
  68.         LONG count = IFFL_GetColorTab(handle, colortable);
  69.         /* Get display information */
  70.         ULONG displayid = IFFL_GetViewModes(handle);
  71.         /* Get picture information */
  72.         struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  73.         struct Window* win = getDrawWin();
  74.         /* Try to adjust the screen to fit */
  75.         if(bmhd)
  76.         {
  77.             struct EasyStruct myreq = { sizeof(struct EasyStruct),
  78.                                                                     0,
  79.                                                                     "Screen Change Confirmation",
  80.                                                                     "The selected image is %ldx%ldx%ld.\n"
  81.                                                                         "Do you wish to change the current\n"
  82.                                                                         "screen to match this?",
  83.                                                                     "Yes|No" };
  84.             /* Only change the GUI if the user wants to */
  85.             if(EasyRequest(win, &myreq, NULL,
  86.                                          bmhd->w, bmhd->h, bmhd->nPlanes))
  87.             {
  88.                 closeGUI();
  89.                 /* If this fails, our local win will then be set to NULL */
  90.                 openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  91.                 win = getDrawWin();
  92.             }
  93.         }
  94.         if(win)
  95.         {
  96.             /* Change screen colours */
  97.             LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  98.             /* If we can load the picture, update window's display */
  99.             if(IFFL_DecodePic(handle, getBitmap()))
  100.             {
  101.                 CopySBitMap(win->WLayer);
  102.                 setModified(FALSE);
  103.             }
  104.             else
  105.                 printf("Error: could not decode IFF picture\n");
  106.         }
  107.         else
  108.             going = FALSE;  /* The only fatal error */
  109.         IFFL_CloseIFF(handle);
  110.     }
  111.     else
  112.         printf("Error: could not open IFF file\n");
  113.     return going;
  114. }
  115.  
  116. /* Open an ASL save file requester */
  117. void save()
  118. {
  119.     /* Another way of saying "allocate if we haven't already" */
  120.     if(savereq ||
  121.         (savereq = (struct FileRequester*)
  122.             AllocAslRequestTags(ASL_FileRequest,
  123.                                                     ASLFR_TitleText,            "Save File",
  124.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  125.                                                     ASLFR_InitialPattern,    "#?.iff",
  126.                                                     ASLFR_InitialFile,        "picture.iff",
  127.                                                     TAG_DONE)))
  128.     {
  129.         struct Window* win = getDrawWin();
  130.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  131.         {
  132.             char filename[MAXFILENAME];
  133.             /* Create complete filename from ASL's dir and file */
  134.             strcpy(filename, savereq->rf_Dir);
  135.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  136.             {
  137.                 /* Make sure our bitmap is the same as the display */
  138.                 SyncSBitMap(win->WLayer);
  139.                 /* Try saving our bitmap, using the screen's colours */
  140.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  141.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  142.                                                     IFFL_COMPR_BYTERUN1))
  143.                 {
  144.                     /* Write an icon for the file */
  145.                     struct DiskObject* dobj = GetDefDiskObject(WBPROJECT);
  146.                     setModified(FALSE);
  147.                     if(dobj)
  148.                     {
  149.                         /* Temporarily change the default tool */
  150.                         char* dtool = dobj->do_DefaultTool;
  151.                         dobj->do_DefaultTool = progName();
  152.                         /* Write out our icon */
  153.                         PutDiskObject(filename, dobj);
  154.                         /* Reinstate the default tool */
  155.                         dobj->do_DefaultTool = dtool;
  156.                         FreeDiskObject(dobj);
  157.                     }
  158.                 }
  159.                 else
  160.                     printf("Error: could not write IFF picture\n");
  161.             }
  162.             else
  163.                 printf("Error: could not make filename\n");
  164.         }
  165.         /* else: requester was cancelled */
  166.     }
  167.     else
  168.         printf("Error: could not allocate ASL (save) file request\n");
  169. }
  170.  
  171. /* Free any requesters that may have been allocated */
  172. void freeReqs()
  173. {
  174.     if(loadreq)
  175.     {
  176.         FreeAslRequest(loadreq);
  177.         loadreq = NULL;
  178.     }
  179.     if(savereq)
  180.     {
  181.         FreeAslRequest(savereq);
  182.         savereq = NULL;
  183.     }
  184. }
  185.  
  186.